<template>
  <div class="app-container">
    <div v-show="!detailInfo.visible && !detailListInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
          :slip-select-request="slipSelectRequest"
        >
        </NewPageFilter>
        <el-button @click="detailListInfo.visible = true">明细页</el-button>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList"> </NewPageFilter>
        <div class="display-flex">
          <el-button @click="preferenceInfo.visible = true">偏好设置</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :check-box="false"
        :tab-index="true"
        :highlightRow="true"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :row-class-name="tableRowClassName"
        @sortChange="sortChange"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-orderNo="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.orderNo
          }}</span>
        </template>
      </TableContainer>
    </div>
    <detail
      v-if="detailInfo.visible"
      :table-id="detailInfo.id"
      v-model:visible="detailInfo.visible"
      @returnBack="getList"
    />
    <SetPreference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      :group-level-show="false"
      @callback="setPreferenceCallback"
    />
    <AdvancedSearch
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      v-model:form-data="advancedSearchInfo.data"
      :field-list="tableInfo.fieldList"
      @callback="advancedSearchCallback"
    />

    <!-- 明细 -->
    <detail-list
      v-if="detailListInfo.visible"
      v-model:visible="detailListInfo.visible"
      @callback="getList"
    ></detail-list>
    <!-- 导出 -->
    <PageDialog
      v-show="excelOrPrintDialogInfo.visible"
      :title="excelOrPrintDialogInfo.title"
      v-model:visible="excelOrPrintDialogInfo.visible"
      :width="excelOrPrintDialogInfo.width"
      :bt-loading="excelOrPrintDialogInfo.btLoading"
      :bt-list="excelOrPrintDialogInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ excelOrPrintDialogInfo.msg }}：
        </div>
        <el-radio-group v-model="excelOrPrintDialogInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </PageDialog>
  </div>
</template>

<script setup name="deliverOrderDetail">
import { fatchTemplateByCode } from '@/api/base.js'
import {
  getDeliverOrderCustomizeListPage,
  excelListDeliverOrder,
  printListDeliverOrder,
} from '@/api/supplier.js'
import { getSupplierAll } from '@/api/core'
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'
import DetailList from './detailList'
import Detail from './details'
const { proxy } = getCurrentInstance()
const detailListInfo = ref({ visible: false })
const clearType = ref(true)
const slipSelectRequest = ref(true)
//导出提示相关配置
const excelOrPrintDialogInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: true,
  btList: [
    { label: '关闭', event: excelOrPrintHandleClose, show: true },
    {
      label: '打印',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})

const modifyInfo = ref({
  visible: false,
  id: '',
  title: '',
})

// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '高级查询', event: advancedSearchShow },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      label: '打印',
      type: 'button',
      event: print,
      show: proxy.$permission('deliverOrderDetail:printOrderList'),
    },
    {
      label: '导出excel',
      type: 'button',
      event: excel,
      show: proxy.$permission('deliverOrderDetail:excelOrderList'),
    },
  ],
})
const page = ref({
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  fieldList: [],
})
const detailInfo = ref({
  visible: false,
  id: '',
})
const preferenceInfo = ref({
  visible: false,
})
const advancedSearchInfo = ref({
  visible: false,
  data: {},
})
function returnModify() {
  if (modifyInfo.value.exportId) {
    getList()
  } else {
    search()
  }
}
//打印
function print() {
  excelOrPrintDialogInfo.value.visible = true
  excelOrPrintDialogInfo.value.type = 'print'
  excelOrPrintDialogInfo.value.msg = '请选择当数据打印范围'
  excelOrPrintDialogInfo.value.btList[1].label = '打印'
  excelOrPrintDialogInfo.value.flag = true
}
//导出
function excel() {
  excelOrPrintDialogInfo.value.visible = true
  excelOrPrintDialogInfo.value.type = 'excel'
  excelOrPrintDialogInfo.value.msg = '请选择数据导出范围'
  excelOrPrintDialogInfo.value.btList[1].label = '导出'
  excelOrPrintDialogInfo.value.flag = true
}
//打印或导出Excel
function checkConfirm() {
  if (excelOrPrintDialogInfo.value.type === 'excel') {
    exportExcelList()
  } else if (excelOrPrintDialogInfo.value.type === 'print') {
    printList()
  }
  excelOrPrintDialogInfo.value.visible = false
  excelOrPrintDialogInfo.value.msg = ''
}
function exportExcelList() {
  let params = {
    ...filterInfo.value.query,
    ...page.value,
    page: excelOrPrintDialogInfo.value.flag,
  }
  excelListDeliverOrder(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function printList() {
  fatchTemplateByCode({
    resourceCode: 'deliverOrderDetail:printOrderList',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp, excelOrPrintDialogInfo.value.flag)
    }
  })
}
function getPrintData(temp, flag) {
  let query = {
    customizeDto: filterInfo.value.query,
  }
  let params = {
    ...query,
    ...page.value,
    page: flag,
  }
  printListDeliverOrder(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
function excelOrPrintHandleClose() {
  excelOrPrintDialogInfo.value.visible = false
}

async function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}

function advancedSearchShow() {
  advancedSearchInfo.value.visible = true
}
async function advancedSearchCallback(obj) {
  filterInfo.value.query.queryMap = {}
  filterInfo.value.query.queryDtoList = [...obj.queryDtoList]
  advancedSearchInfo.value.data = obj.data
  await getList()
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
}
// 获取列表
async function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  // 重置分页
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryDtoList = []
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}

//展示明细
function showDetail(row) {
  detailInfo.value.id = row.id
  detailInfo.value.visible = true
}
function tableRowClassName({ row }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && row.id === Number(rowId)) {
    return 'check-row'
  }
  return ''
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}
function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'supplierDeliverOrderList-com.slip.spd.entity.vo.supplier.DeliverOrderVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.value.api = getDeliverOrderCustomizeListPage
      getList()
    }
  })
}
function sortChange(column) {
  if (filterInfo.value.query.customizeUserVO.head.sortType === 0) {
    let arr = proxy.$fn.sortChange(column, filterInfo.value.sortArr)
    filterInfo.value.sortArr = arr
    filterInfo.value.query.sortString = arr.length ? arr.toString() : ''
    getList()
  }
}

let obj = {
  statusValue: {
    label: '状态',
    value: 'statusValue',
    asValue: 'status',
    componentType: 'select-model',
    code: 'deliver_order_status',
    show: false,
    hidden: true,
  },
  supplierName: {
    label: '供应商',
    value: 'supplierName',
    asValue: 'orgId',
    componentType: 'slip-select',
    api: getSupplierAll,
    query: {
      supplierAuth: 'supplierId',
      sort: 'create_time desc',
    },
    clearable: true,
    filterable: true,
    keyField: 'supplierName',
    valueField: 'supplierId',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
getCustomize()
</script>

<style scoped>
.app-container {
  height: calc(100vh - 86px);
}
</style>
